package com.sfac.springBoot.modules.account.dao;

import com.sfac.springBoot.modules.account.entity.User;
import com.sfac.springBoot.modules.common.vo.Search;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserDao {
    @Select("select * from account_user where user_name=#{userName}")
    User login(String userName);
    @Delete("delete from account_user where id=#{id}")
    void deleteUser(int id);
//    @Select("select * from account_user where id=#{id}")
//    User getUserById(int id);
    @Insert("insert into account_user values(null,#{createDate},#{userName},#{password},null)")
    void insertUser(User user);
    @Update("update account_user set user_name=#{userName} where id=#{id}")
    void updateUser(User user);
    @Select("select * from account_user where id = #{id}")
    @Results(id = "userResults", value = {
            @Result(property="id", column = "id"),
            @Result(property = "roles",
                    column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.sfac.springBoot.modules.account.dao.RoleDao.getRolesByUserId"))
    })
    User getUserById(int id);
    @Select("<script>"
            + "select * from account_user "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (user_name like '%${keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='sort != \"\" and sort != null'>"
            + " order by ${sort} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<User> loadAllUsers(Search Search);
}
